<template>
    <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
            <a-form :form="form" slot="detail">
                <a-row>
                    <a-col :span="24">

                        <a-form-item label="菜单类型" required :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-radio-group @change="onChangeMenuType"
                                           v-decorator="['type',{initialValue : localMenuType}]"
                                           placeholder="请选择机构类型">
                                <a-radio value="A">一级菜单</a-radio>
                                <a-radio value="B">子菜单</a-radio>
                                <a-radio value="C">按钮</a-radio>
                            </a-radio-group>
                        </a-form-item>

                        <a-form-item
                                v-show="localMenuType != 'A'"
                                label="上级菜单"
                                :labelCol="labelCol"
                                :wrapperCol="wrapperCol"
                                :validate-status="validateStatus"
                                :hasFeedback="true"
                                :required="true">
                            <span slot="help">{{ validateStatus=='error'?'请选择上级菜单':'&nbsp;&nbsp;' }}</span>
                            <a-tree-select
                                    style="width:100%"
                                    :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
                                    :treeData="treeData"
                                    v-model="model.pid"
                                    placeholder="请选择父级菜单"
                                    @change="handleParentIdChange">
                            </a-tree-select>
                        </a-form-item>

                        <a-form-item :label="menuLabel" required :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['name']" placeholder="菜单名称"></a-input>
                        </a-form-item>

                        <a-form-item label="授权标识" required :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="localMenuType === 'C'" >
                            <a-input v-decorator="['authKey']" placeholder="授权标识"></a-input>
                        </a-form-item>

                        <a-form-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <j-image-upload :isMultiple="true" v-decorator="['img']"></j-image-upload>
                        </a-form-item>

                        <a-form-item label="应用图标" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['icon']" placeholder="应用图标"></a-input>
                        </a-form-item>

                        <a-form-item label="颜色" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input type="color" v-decorator="['color']" placeholder="请输入颜色"></a-input>
                        </a-form-item>

                        <a-form-item label="应用地址" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="localMenuType === 'B'" >
                            <a-input v-decorator="['url',{initialValue : '#'}]" placeholder="应用地址"></a-input>
                        </a-form-item>
                        <a-form-item label="是否显示跳转" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="localMenuType === 'B'">
                            <a-switch v-model="showBtnUrl" />
                        </a-form-item>

                        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input-number v-decorator="['sn',{initialValue:'0'}]" placeholder="请输入排序"
                                            style="width: 100%"/>
                        </a-form-item>
                    </a-col>
                    <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
                        <a-button @click="submitForm">提 交</a-button>
                    </a-col>
                </a-row>
            </a-form>
        </j-form-container>
    </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JMultiSelectTag from '@/components/dict/JMultiSelectTag'
  import { getAppAllMenuList } from '../../../api/app'

  export default {
    name: 'HcBackRoleMenuForm',
    components: {
      JFormContainer,
      JImageUpload,
      JMultiSelectTag
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: () => {
        },
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data() {
      return {
        form: this.$form.createForm(this),
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {},
        url: {
          add: '/hc/hcBackRoleMenu/add',
          edit: '/hc/hcBackRoleMenu/edit',
          queryById: '/hc/hcBackRoleMenu/queryById'
        },

        model: {},
        localMenuType: 'A',
        menuLabel: '菜单名称',
        validateStatus: '',
        showBtnUrl: true,
        treeData: []
      }
    },
    computed: {
      formDisabled() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return true
          }
        }
        return false
      }
    },
    created() {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData()
    },
    methods: {
      loadTree: async function() {
        this.treeData = await getAppAllMenuList({type : 'A,B'});
      },
      add() {
        this.localMenuType = 'A'
        this.edit({ pid: 0, type: 'A' })
      },
      addSubMenu(record) {
        this.loadTree()
        this.form.resetFields()
        this.showBtnUrl = true
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'pid', 'type'))
        })
      },
      edit(record) {
        this.form.resetFields()
        this.loadTree()
        if (record.showarrow === '1') {
          this.showBtnUrl = true
        } else {
          this.showBtnUrl = false
        }
        this.localMenuType = record.type
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'pid', 'createBy', 'authKey', 'createTime', 'updateBy', 'updateTime', 'name', 'url', 'icon', 'img', 'color', 'showarrow', 'type', 'sn'))
        })
      },

      //渲染流程表单数据
      showFlowData() {
        if (this.formBpm === true) {
          let params = { id: this.formData.dataId }
          getAction(this.url.queryById, params).then((res) => {
            if (res.success) {
              this.edit(res.result)
            }
          })
        }
      },
      submitForm() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            values['showarrow'] = this.showBtnUrl ? '1' : '0'
            values['pid'] = this.model.pid || 0;

            if (this.localMenuType === 'A') {
              values['pid'] = 0
            }
            let formData = Object.assign(this.model, values)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          }

        })
      },

      onChangeMenuType(e) {
        this.localMenuType = e.target.value
        if (e.target.value == 'C') {
          this.menuLabel = '按钮(权限)名称'
        } else {
          this.menuLabel = '菜单名称'
        }
      },

      handleParentIdChange(value) {
        console.log(value)
        if (!value) {
          this.validateStatus = 'error'
        } else {
          this.validateStatus = 'success'
        }
      }

    }
  }
</script>
